# author: mikeqiu95
# date: 2022-8-21 17:08
# 说明: 创建图形日期选取器
# IDE: Visual Studio Code
# Software Version: 1.0
# PowerShell: 5.1
# 帮助文档: https://docs.microsoft.com/zh-cn/powershell/scripting/samples/creating-a-graphical-date-picker?view=powershell-7.2


Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object Windows.Forms.Form -Property @{
    StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
    Size = New-Object Drawing.Size 243,230
    Text = 'Select a Date'
    Topmost = $true
}

$calendar = New-Object Windows.Forms.MonthCalendar -Property @{
    ShowTodayCircle = $false
    MaxSelectionCount = 1
}
$form.Controls.Add($calendar)

# New-Object的意思就是在这里创建一个实际的Windows.Forms.Button
# 像建筑房子一样，这里就New出来了，你可以为所欲为
# 使用这种Property的方式可以方便理解
$okButton = New-Object Windows.Forms.Button -Property @{
    Location = New-Object Drawing.Point 38, 165
    Size = New-Object Drawing.Size 75, 23
    Text = 'Ok'
    DialogResult = [Windows.Forms.DialogResult]::OK
}
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object Windows.Forms.Button -Property @{
    Location = New-Object Drawing.Point 113, 165
    Size = New-Object Drawing.Size 75, 23
    Text = 'Cancel'
    DialogResult = [Windows.Forms.DialogResult]::Cancel
}
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$result = $form.ShowDialog()

if($result -eq [Windows.Forms.DialogResult]::OK){
    $date = $calendar.SelectionStart
    Write-Host "Data selected: $($date.ToShortDateString())"
}
